package com.imooc.maokeyang.alllearning.security.services;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imooc.maokeyang.alllearning.domain.entity.UserDO;
import com.imooc.maokeyang.alllearning.mapper.UserMapper;
import com.imooc.maokeyang.alllearning.security.entity.JwtUser;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class UserDetailsServiceImpl implements UserDetailsService {

    private final UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {


        //QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();

        //queryWrapper.eq("username", username);

        //UserDO user = userMapper.selectOne(queryWrapper);

        UserDO user = userMapper.findRolesWithUser(username);

        if (user == null) {
            throw new RuntimeException("User NotFound :" + username);
        }


        return JwtUser.build(user);
    }
}
